package com.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @Author: Ren(任佳宝)
 * @Description: ElsaticSearchConfig
 * @Date: 2025/1/15 下午12:02
 * @Version: 1.0
 */
@Configuration
@RefreshScope
public class ElasticSearchConfig {

    @Value("${es.hostName}")
    private String hostName;

    @Value("${es.port}")
    private Integer port;

    @Bean
    @Primary
    public RestHighLevelClient restHighLevelClient() {
        RestClientBuilder http = RestClient
                .builder(new HttpHost(hostName, port, "http"));
        return new RestHighLevelClient(http);
    }

    public static final RequestOptions COMMON_OPTIONS;

    static {

        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();

        COMMON_OPTIONS = builder.build();
    }


}
